
local BaseSprite = class('BaseSprite',function()
	return display.newNode()
end)

--[[
	属性：
	filename:保存纹理图片名
	x,y:世界保存坐标值
	tag:节点tag及子节点tag
	col:矩阵列坐标,原点左下角
	row:矩阵行坐标
	oldCol:记录移动前的坐标（修正矩阵时的）
	oldRow:
	isWall:表示墙，用于阻止移动
	sytle:
		0:默认值
		1,2,3,4分别表示：mangjuo,putao,taozhi,xingzhi,
		10: 水果篮子,basket
		11:	墙,wall

	effect，effect2:表示带有特效属性，horizontal水平特效，vertical垂直特效，bomb爆炸特效,basket
	childSpriteTag:子精灵tag值
	selectedChildTag:选中效果的子精灵tag值
	conRemove:标记是否可以除移除
	removeSytle:花篮效果，要移除的类形
	isSelected:
	destoryEffectActionTag:标记特效消除动画，以便不执行下坠动作
	nMaxActions: 			标记下坠动画数量
	landPositions:(col,row) 
]]
function BaseSprite:ctor(argc)
	self.filename = argc.filename
	self.conRemove = true
	self.wallNumber = 0
	self.x,self.y = BaseSprite.toPosition(argc.column,argc.row)
	self:setLocalZOrder(9)
	--print(_filename)
	local _sprite = display.newSprite('#' .. self.filename)
	local zOrder = argc.zOrder or 10
	_sprite:addTo(self)
	_sprite:setLocalZOrder(zOrder)
	
	self:pos(self.x,self.y)
	self.col = argc.column
	self.row = argc.row 
	self.oldCol = argc.column
	self.oldRow = argc.row 
	self.tag = self.row*100+self.col
	self.childSpriteTag = 131313
	self.selectedChildTag = 131314
	self.destoryEffectActionTag = 131315
	_sprite:setTag(self.childSpriteTag)
	self:setTag(self.tag)
	--printf('self.tag=%d',self.tag)

	if self.filename then
		local fn = string.sub(self.filename,1,string.find(self.filename,'.png')-1)
		--print(fn)
		if fn == 'mangjuo' then self.sytle = 1
		elseif fn == 'putao' then self.sytle = 2
		elseif fn == 'taozhi' then self.sytle = 3
		elseif fn == 'xingzhi' then self.sytle = 4
		elseif fn == 'caomei' then self.sytle = 5
		elseif fn == 'basket' then self.sytle = 10
		elseif fn == 'wall' then 
			self.sytle = 11
			baseWall[self.row][self.col] = 1
			self.wallNumber = 3
		end
	else
		self.sytle = 0
	end

	self.effect = nil
	self.effect2 = nil
	self.isSelected = false
	self.removeSytle = nil
	self.nMaxActions = 0
	self.gLandNumber = 0
	self.landPositions = nil
	self.intentToPoint = nil
end

function BaseSprite.default(argc)
	local _arg = {}
	_arg.x = argc.x
	_arg.y = argc.y
	_arg.column = argc.column
	_arg.row = argc.row

	--if not _arg.x or not _arg.y then
	--	_arg.x,_arg.y = BaseSprite.toPosition(_arg.column,_arg.row)
	--end

	_arg.tag = argc.tag or argc.row*100+argc.column
	_arg.zOrder = 0
	return BaseSprite.new(_arg)
end

function BaseSprite.toMatrix(x,y)
	local c,r= nil,nil
	if(x>=320 and x<=830 and y>=32 and y<=604) then
		c = x-320
		r = y-32
		c = c/64
		r = r/64
		c = math.floor(c)
		r = math.floor(r)
		c = c + 1
		r = r + 1
	end
	return c,r
end

function BaseSprite.toPosition(col,row)
	local x,y = 0,0
	x=320+32+64*(col-1)
	y=64+64*(row-1)
	return x,y
end

function BaseSprite.changeToTag(col,row)
	return row*100+col
end

function BaseSprite:cancleSelectedEffect()
	self:getChildByTag(self.selectedChildTag):removeSelf()
	self.isSelected = false
end

function BaseSprite:setSelectedEffect()
	local sytle = self.sytle
	--assert(sytle ~= 10,'Now have not set bakset effect of select')
	local child = display.newSprite('#' .. SPRITE_SELECT_SOURCES[sytle]):addTo(self)
	child:setTag(self.selectedChildTag)
	self.isSelected = true
end

function BaseSprite:printBaseSpriteInfo()
	printf('... ... my col=%d,row=%d',self.col,self.row)
	printf('... ... my texture file =%s',self.filename)
	printf('... ... my sytle = %s',self.sytle)
	printf('... ... my effect = %s',self.effect)
end

local points = {
		right = {
			--右移
			cc.p(64,0),
			cc.p(-6,0),
			cc.p(6,0),
			cc.p(-3,0),
			cc.p(3,0),
		},
		left = {
			--左移
			cc.p(-64,0),
			cc.p(6,0),
			cc.p(-6,0),
			cc.p(3,0),
			cc.p(-3,0),
		},
		up = {
			--上移
			cc.p(0,64),
			cc.p(0,-6),
			cc.p(0,6),
			cc.p(0,-3),
			cc.p(0,3),
		},
		down = {
			--下移
			cc.p(0,-64),
			cc.p(0,6),
			cc.p(0,-6),
			cc.p(0,3),
			cc.p(0,-3),
		}
}

--[[
设置默认的精灵
]]
function BaseSprite:doActionLeft(needMoveBack)
	--print('... BaseSprite doActionLeft ...')

	local moveLeft
	if not needMoveBack then  										--不需要加称，则删除本次对象
		--gSprites[self.row][self.col] = nil
		--self.col = self.col-1 										--更新内部矩阵坐标
		moveLeft= cc.Sequence:create(
			cc.MoveBy:create(0.12,points.left[1]),
			cc.MoveBy:create(0.06,points.left[2]),
			cc.MoveBy:create(0.04,points.left[3]),
			cc.CallFunc:create(function()
				--[[
				if(needCallBack) then 
					self.tag = self.tag + 100
					--self.row = self.row + 1
					self:setTag(self.tag)
					--print('... reset Tag=%d',self.tag)
					gEventDispatchCentre:dispatchEvent({name = 'finishAnimation',tager=self,direction='down',isMoveBack=isMoveBack})
				end	
				]]
				gSprites[self.row][self.col]=self
				gEventDispatchCentre:dispatchEvent({name = 'cleanupDestoryPool'})
			end)
		)
	else
		moveLeft = cc.Sequence:create(
			cc.MoveBy:create(0.12,points.left[1]),
			cc.MoveBy:create(0.06,points.right[1]),
			cc.MoveBy:create(0.04,points.right[2]),
			cc.MoveBy:create(0.04,points.right[3])
		)
	end

	self:runAction(moveLeft)

end

function BaseSprite:doActionRight(needMoveBack)
	--print('... BaseSprite doActionRight ...')

	local moveRight
	if not needMoveBack then  										--不需要加称，则删除本次对象
		--gSprites[self.row][self.col] = nil
		--self.col = self.col+1 										--更新内部矩阵坐标
		moveRight= cc.Sequence:create(
			cc.MoveBy:create(0.12,points.right[1]),
			cc.MoveBy:create(0.06,points.right[2]),
			cc.MoveBy:create(0.04,points.right[3]),
			cc.CallFunc:create(function()
				gEventDispatchCentre:dispatchEvent({name = 'cleanupDestoryPool'})
			end)
		)
	else
		moveRight= cc.Sequence:create(
			cc.MoveBy:create(0.12,points.right[1]),
			cc.MoveBy:create(0.06,points.left[1]),
			cc.MoveBy:create(0.04,points.left[2]),
			cc.MoveBy:create(0.04,points.left[3])
		)
	end

	self:runAction(moveRight)

end

function BaseSprite:doActionUp(needMoveBack)
	--print('... BaseSprite doActionUp ...')

	local moveUp
	if not needMoveBack then  										--不需要加称，则删除本次对象
		--gSprites[self.row][self.col] = nil
		--self.row = self.row+1 										--更新内部矩阵坐标
		moveUp= cc.Sequence:create(
		cc.MoveBy:create(0.12,points.up[1]),
		cc.MoveBy:create(0.06,points.up[2]),
		cc.MoveBy:create(0.04,points.up[3]),
		cc.CallFunc:create(function()
			gEventDispatchCentre:dispatchEvent({name = 'cleanupDestoryPool'})
		end)
		)
	else
		moveUp= cc.Sequence:create(
			cc.MoveBy:create(0.12,points.up[1]),
			cc.MoveBy:create(0.06,points.down[1]),
			cc.MoveBy:create(0.04,points.down[2]),
			cc.MoveBy:create(0.04,points.down[3])
		)
	end

	self:runAction(moveUp)

end

function BaseSprite:doActionDown(needMoveBack)
	--print('... BaseSprite doActionDown ...')

	local moveDown
	if not needMoveBack then  										--不需要加称，则删除本次对象
		--gSprites[self.row][self.col] = nil
		--self.row = self.row-1 										--更新内部矩阵坐标
		moveDown= cc.Sequence:create(
		cc.MoveBy:create(0.12,points.down[1]),
		cc.MoveBy:create(0.06,points.down[2]),
		cc.MoveBy:create(0.04,points.down[3]),
		cc.CallFunc:create(function()
			gEventDispatchCentre:dispatchEvent({name = 'cleanupDestoryPool'})
			gTouchEnable = true
		end)
		)
	else
		moveDown= cc.Sequence:create(
			cc.MoveBy:create(0.12,points.down[1]),
			cc.MoveBy:create(0.12,points.up[1]),
			cc.MoveBy:create(0.06,points.up[2]),
			cc.MoveBy:create(0.04,points.up[3]),
			cc.CallFunc:create(function()
				gTouchEnable = true
				end
			)
		)
	end

	self:runAction(moveDown)
end

--[[
score:要显示的分值
notDisplayscore:是否要显示分值
]]
function BaseSprite:doRemoveSelf(score,notDisplayscore,changeToEffect)
	---[[
	--if self.isSelected then BaseSprite.cancleSelectedEffect(self.col,self.row) end
	if not self.conRemove then return end 								--
	self.conRemove = false
	if self.sytle == 10 then 						--如果是篮子，直接执行效果
		gEventDispatchCentre:dispatchEvent({name='doDestoryEffect', effect={self.effect,self.effect2},point={col=self.col,row=self.row }} )
		return
	end
	local scene = display.getRunningScene()
	local par = cc.ParticleSystemQuad:create('destroyEffect.plist')
	self:addChild(par)
	--printf('BaseSprite:doRemoveSelf inside row=%d,col=%d',self.row,self.col)
	local seq = cc.Sequence:create(
		cc.ScaleTo:create(0.2,0.1),
		cc.CallFunc:create(function()
			local row = self.row
			local col = self.col
			gHintTimer = 0
			gTouchEnable = true
			if self.effect then 
				--printf('BaseSprite:doRemoveSelf : on self.effect=%s',self.effect)
				gEventDispatchCentre:dispatchEvent({name='doDestoryEffect', effect={self.effect,self.effect2},point={col=self.col,row=self.row }} )
			end
			--print('BaseSprite:doRemoveSelf changeToEffect=%s',changeToEffect)
			if changeToEffect or (self.effect2 and self.effect)then
				--print('BaseSprite:doRemoveSelf changeToEffect=%s',changeToEffect)
				self:getChildByTag(self.childSpriteTag):runAction(cc.ScaleTo:create(0.3,1.0))
				if not self.effect2 then self:setEffect(changeToEffect) end
				self.conRemove = true
			else
				--printf('BaseSprite:doRemoveSelf removeSelf row=%d,col=%d',row,col)
				if self.gLandNumber > 0 then gLandNumber = gLandNumber - self.gLandNumber end
				if self.landPositions then gLandPositions[self.landPositions.row][self.landPositions.col] = 0 end
				self:removeSelf()
				gSprites[row][col] = nil
				if self.intentToPoint then intentToPoint[self.intentToPoint.row][self.intentToPoint.col]=0 end
				--gEventDispatchCentre:dispatchEvent({name='landSprites',col=col})
			end
		end)
	)
	self:getChildByTag(self.childSpriteTag):runAction(seq)
	if not changeToEffect and self.effect2 then
		seq:setTag(self.destoryEffectActionTag)
	end

	if not notDisplayscore then
		local x,y = BaseSprite.toPosition(self.col,self.row)
																	
		local dot = cc.ParticleSystemQuad:create('dot.plist')
		dot:addTo(scene)
		dot:pos(x,y)
		dot:runAction(cc.Sequence:create(
			cc.BezierTo:create(0.5,{cc.p(x,y),cc.p(100+(x-100)/4,640),cc.p(64,display.cy+64+48*(5-self.sytle))}),
			cc.RemoveSelf:create()
			)
		)
		local text = cc.ui.UILabel.new({
	            UILabelType = 2, text = score, size = 14})
	        :align(display.CENTER, x, y)
	        :addTo(scene)
	      text:setTag(self.tag*100)
	    local seq2 = cc.Sequence:create(
	    	cc.MoveBy:create(1.2,cc.p(0,64)),
	    	cc.RemoveSelf:create()
	    )
	    scene.score = scene.score + score
	    scene.scoreLable:setString(scene.score)
	    scene.loadpar:setParent(scene.score/scene.total*100)
	    text:runAction(seq2)
	    ---[[
	    if self.sytle == 1 then
	    	scene.sytle1Number = scene.sytle1Number + 1
    		scene.sytle1NumberLable:setString(scene.sytle1Number)
	    elseif self.sytle == 2 then
	    	scene.sytle2Number = scene.sytle2Number + 1
	    	scene.sytle2NumberLable:setString(scene.sytle2Number)
    	elseif self.sytle == 3 then
    		scene.sytle3Number = scene.sytle3Number + 1
    		scene.sytle3NumberLable:setString(scene.sytle3Number)
		elseif self.sytle == 4 then
			scene.sytle4Number = scene.sytle4Number + 1
			scene.sytle4NumberLable:setString(scene.sytle4Number)
		elseif self.sytle == 5 then
			scene.sytle5Number = scene.sytle5Number + 1
			scene.sytle5NumberLable:setString(scene.sytle5Number)
		end
		--]]
	end

	--如果四周有墙，则做销毁动作
	local row = self.row
	local col = self.col
	if row>1 and baseWall[row-1][col] == 1 then gWallSprites[row-1][col]:destoryWall() end
	if row<9 and baseWall[row+1][col] == 1 then gWallSprites[row+1][col]:destoryWall() end
	if col>1 and baseWall[row][col-1] == 1 then gWallSprites[row][col-1]:destoryWall() end
	if col<8 and baseWall[row][col+1] == 1 then gWallSprites[row][col+1]:destoryWall() end

end

--[[
	判断是否可以移动对象
]]
function BaseSprite:isCanMove(dir)
	local str = string.upper(dir)
	local col,row = self.col,self.row
	if self.effect2 and not self.effect then
		self.effect = self.effect2
		self.effect2 = nil
	end

	if str == 'UP' then
		--printf('isCanMove,up row=%d',row)
		if row == 9 then return false end 									--到顶了，不可再移动
		if baseMap[row+1][col] ~= 1 then return false end 					--上面为无效格子，不可移动
		if not gSprites[row+1][col] then return false end                   --上面不存在对象，不可移动
		if gSprites[row+1][col].sytle == 11 or gSprites[row+1][col].sytle == 0 then return false end --如果上面的对象是墙或者无效格子，不可移动
		
		local effect,effect2 =  gSprites[row][col].effect,gSprites[row+1][col].effect
		if effect == 'basket' or effect2 == 'basket' then 										     --任何一个特效为basket则可移动
			return true
		elseif effect and effect2 then 																 --都存在其它特效，则可移动
			return true
		else
			if gSprites[row+1][col].sytle == gSprites[row][col].sytle then 							 --如果上面是相同的东西不可移动
				return false 																 			 
			else
				return true
			end
		end
	elseif str == 'DOWN' then
		if row == 1 then return false end
		if baseMap[row-1][col] ~= 1 then return false end
		if not gSprites[row-1][col] then return false end
		if gSprites[row-1][col].sytle == 11 or gSprites[row-1][col].sytle == 0 then return false end
		
		local effect,effect2 =  gSprites[row][col].effect,gSprites[row-1][col].effect
		if effect == 'basket' or effect2 == 'basket' then 										     --任何一个特效为basket则可移动
			return true
		elseif effect and effect2 then 																 --都存在其它特效，则可移动
			return true
		else
			if gSprites[row-1][col].sytle == gSprites[row][col].sytle then 							 --如果上面是相同的东西不可移动
				return false 																 			 
			else
				return true
			end
		end
	elseif str == 'LEFT' then
		if col == 1 then return false end
		if baseMap[row][col-1] ~= 1 then return false end
		if not gSprites[row][col-1] then return false end
		if gSprites[row][col-1].sytle == 11 or gSprites[row][col-1].sytle == 0 then return false end
		
		local effect,effect2 =  gSprites[row][col].effect,gSprites[row][col-1].effect
		if effect == 'basket' or effect2 == 'basket' then 										     --任何一个特效为basket则可移动
			return true
		elseif effect and effect2 then 																 --都存在其它特效，则可移动
			return true
		else
			if gSprites[row][col-1].sytle == gSprites[row][col].sytle then 							 --如果上面是相同的东西不可移动
				return false 																 			 
			else
				return true
			end
		end
	elseif str == 'RIGHT' then
		if col == 8 then return false end
		if baseMap[row][col+1] ~= 1 then return false end
		if not gSprites[row][col+1] then return false end
		if gSprites[row][col+1].sytle == 11 or gSprites[row][col+1].sytle == 0 then return false end
		local effect,effect2 =  gSprites[row][col].effect,gSprites[row][col+1].effect
		if effect == 'basket' or effect2 == 'basket' then 										     --任何一个特效为basket则可移动
			return true
		elseif effect and effect2 then 																 --都存在其它特效，则可移动
			return true
		else
			if gSprites[row][col+1].sytle == gSprites[row][col].sytle then 							 --如果上面是相同的东西不可移动
				return false 																 			 
			else
				return true
			end
		end
	else
		return false
	end
end

--[[
同EventDispatchCentre里的judgeItem函数
]]
function BaseSprite:judgeItem()
	local row,col

	row = self.row
	col = self.col
	--printf('BaseSprite:judgeItem self.row=%d,self.col=%d,sprites.row=%d,sprites.col=%d',self.row,self.col,gSprites[row][col].row,gSprites[row][col].col)
	if baseMap[row][col] ~= 1 then return end
	if baseWall[row][col] == 1 then return end
	local horizontal,vertical = 0,0

	--printf('... judgeItem tag=%d...',tag)
	--local sprite = gSprites[row][col]
	--if not sprite then print('not have sprite in judgeItme') end
	--assert(sprite,'not have sprite!')
	local sytle =  self.sytle 											--当前格子的类型
	local vPosition = {began=row,ended=row} 								--记录相同格子的起止位置,began,ended
	local hPosition = {began=col,ended=col}

	for i=col+1,8 do 														--水平方向，递增查找
		if baseMap[row][i] ~= 1 then break end
		if baseWall[row][i] == 1 then break end
		local sp = gSprites[row][i]
		if not sp then break end
		--printf('BaseSprite:judgeItem horizontal down i=%d,sytle1=%d,sytle2=%d',i,sytle,sp.sytle)
		if sp.sytle == sytle then 
			horizontal = horizontal + 1
			hPosition.ended = i
			--printf('BaseSprite:judgeItem equal,sytle = %s,col=%d,horizontal up number=%d',sytle,col,horizontal)
		else
			break
		end
	end

	for i=col,1,-1 do 														--水平方向，递减查找（包含本身）
		--printf('BaseSprite:judgeItem horizontal down i=%d',i)
		if baseMap[row][i] ~= 1 then break end
		if baseWall[row][i] == 1 then break end
		local sp = gSprites[row][i]
		if not sp then break end
		--printf('BaseSprite:judgeItem horizontal down i=%d,sytle1=%d,sytle2=%d',i,sytle,sp.sytle)
		if sp.sytle == sytle then 
			horizontal = horizontal + 1
			hPosition.began = i
			--printf('BaseSprite:judgeItem equal,sytle = %s,horizontal down number=%d',sytle,horizontal)
		else
			break
		end
	end

	for i=row+1,9 do 														--垂直方向，递增查找
		if baseMap[i][col] ~= 1 then break end
		if baseWall[i][col] == 1 then break end
		gSprites[i] = gSprites[i] or {}
		local sp = gSprites[i][col]
		if not sp then break end
		if sp.sytle == sytle then 
			vertical = vertical + 1
			vPosition.ended = i
		else
			break
		end
	end

	for i=row,1,-1 do 														--垂直方向，递减查找（包含本身）
		--printf('judgeItem getChildByTag=%d',BaseSprite.changeToTag(col,i))
		if baseMap[i][col] ~= 1 then break end
		if baseWall[i][col] == 1 then break end
		local sp = gSprites[i][col]
		if not sp then break end
		if sp.sytle == sytle then 
			vertical = vertical + 1
			vPosition.began = i
		else
			break
		end
	end

	return horizontal,vertical,hPosition,vPosition
end

function BaseSprite:setEffect(effectName,notDisplayscore)
	local score
	self.effect = effectName
	if effectName == 'basket' then
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(11)
		local tag = self.childSpriteTag
		self:addChild(par)
		self:removeChildByTag(tag)
		self.sytle = 10
		local child = display.newSprite('#basket.png'):addTo(self)
		child:setTag(tag)
		child:setScale(0.2)
		child:setLocalZOrder(2)
		local scale = cc.ScaleTo:create(0.6,1.0)
		child:runAction(scale)
		score = 300
	elseif effectName == 'bomb' then
		score = 180
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(11)
		self:addChild(par)
	elseif effectName == 'horizontal' then
		score = 120
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(12)
		self:addChild(par)
		local sp = display.newSprite('#effect.png')
		sp:setLocalZOrder(11)
		sp:setScale(0.8)
		sp:setOpacity(128)
		self:addChild(sp)
	else
		score = 120
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(12)
		self:addChild(par)
		local sp = display.newSprite('#effect.png')
		sp:setRotation(90)
		sp:setScale(0.8)
		sp:setLocalZOrder(11)
		sp:setOpacity(128)
		self:addChild(sp)
	end

	--[[
	if not notDisplayscore then
		local x,y = BaseSprite.toPosition(self.col,self.row)
		local tag = self.tag
		local text = cc.ui.UILabel.new({
	            UILabelType = 2, text = score, size = 14})
	        :align(display.CENTER, x, y)
	        :addTo(scene)
	      text:setTag(tag*100)
	    local seq2 = cc.Sequence:create(
	    	cc.MoveBy:create(1.2,cc.p(0,64)),
	    	cc.RemoveSelf:create()
	    )
	    text:runAction(seq2)
	end
	]]

end

function BaseSprite:destoryWall()
	local child = self:getChildByTag(self.childSpriteTag)
	local n = child:getNumberOfRunningActions()
	if self.wallNumber <= 1 then
		self.wallNumber = 0 
		if n ~= 0 then return end
		if gStopSoundEffect then audio.playSound('destoryWall.mp3') end
		child:runAction(cc.Sequence:create(
			cc.ScaleBy:create(0.2,1.2),
			cc.ScaleBy:create(0.3,0.1),
			cc.CallFunc:create(function( )
				baseWall[self.row][self.col] = 0
				if self.col> 1 then baseCanTurnMap[self.row][self.col-1][2] = 0  end											--左边点不可右移
				if self.col<8 then baseCanTurnMap[self.row][self.col+1][1] = 0 	end									--右边点不可左移
				--墙消失后，判断当前点的左右两边是否墙或者空，有的话标记当前点右左右移动，同时清除左右两边的移动标记
				if self.col> 1 and baseMap[self.row][self.col-1]==0 or baseWall[self.row][self.col-1]==1 then
					baseCanTurnMap[self.row][self.col][1] = 1	
				end
				if self.col<8 and baseMap[self.row][self.col+1]==0 or baseWall[self.row][self.col+1]==1 then   --是否为有效点
					baseCanTurnMap[self.row][self.col][2] = 1
				end
				self:removeSelf()
			end)
		))
	elseif self.wallNumber == 2 then
		self.wallNumber = self.wallNumber - 1
		if n ~= 0 then return end
		if gStopSoundEffect then audio.playSound('destoryWall.mp3') end
		child:removeSelf()
		child = display.newSprite('#' .. SPRITE_WALL_SOURCES[1]):addTo(self)
		child:setTag(self.childSpriteTag)
		child:runAction(cc.Sequence:create(
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,-10),
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,-10),
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,0),
			cc.CallFunc:create(function( )
				if self.wallNumber <= 0 then
					if gStopSoundEffect then audio.playSound('destoryWall.mp3') end
					child:removeSelf()
					child = display.newSprite('#' .. SPRITE_WALL_SOURCES[1]):addTo(self)
					child:setTag(self.childSpriteTag)
					child:runAction(cc.Sequence:create(
						cc.ScaleBy:create(0.2,1.2),
						cc.ScaleBy:create(0.3,0.1),
						cc.CallFunc:create(function( )
							baseWall[self.row][self.col] = 0
							gSprites[self.row] = gSprites[self.row] or {}
							gSprites[self.row][self.col] = false
							if self.col> 1 then baseCanTurnMap[self.row][self.col-1][2] = 0  end											--左边点不可右移
							if self.col<8 then baseCanTurnMap[self.row][self.col+1][1] = 0 	end									--右边点不可左移
							--墙消失后，判断当前点的左右两边是否墙或者空，有的话标记当前点右左右移动，同时清除左右两边的移动标记
							if self.col> 1 and baseMap[self.row][self.col-1]==0 or baseWall[self.row][self.col-1]==1 then
								baseCanTurnMap[self.row][self.col][1] = 1	
							end
							if self.col<8 and baseMap[self.row][self.col+1]==0 or baseWall[self.row][self.col+1]==1 then   --是否为有效点
								baseCanTurnMap[self.row][self.col][2] = 1
							end
							self:removeSelf()
						end)
						)
					)
				end
			end)
			)
		)
	elseif self.wallNumber == 3 then
		if gStopSoundEffect then audio.playSound('destoryWall.mp3') end
		self.wallNumber = self.wallNumber - 1
		child:removeSelf()
		child = display.newSprite('#' .. SPRITE_WALL_SOURCES[2]):addTo(self)
		child:setTag(self.childSpriteTag)
		child:runAction(cc.Sequence:create(
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,-10),
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,-10),
			cc.RotateTo:create(0.06,10),
			cc.RotateTo:create(0.12,0),
			cc.CallFunc:create(function( )
				if self.wallNumber == 1 then
					child:removeSelf()
					child = display.newSprite('#' .. SPRITE_WALL_SOURCES[1]):addTo(self)
					child:setTag(self.childSpriteTag)
					child:runAction(cc.Sequence:create(
						cc.RotateTo:create(0.06,10),
						cc.RotateTo:create(0.12,-10),
						cc.RotateTo:create(0.06,10),
						cc.RotateTo:create(0.12,-10),
						cc.RotateTo:create(0.06,10),
						cc.RotateTo:create(0.12,0),
						cc.CallFunc:create(function( )
							if self.wallNumber <= 0 then
								if gStopSoundEffect then audio.playSound('destoryWall.mp3') end
								--child:removeSelf()
								--child = display.newSprite(SPRITE_WALL_SOURCES[1]):addTo(self)
								--child:setTag(self.childSpriteTag)
								child:runAction(cc.Sequence:create(
									cc.ScaleBy:create(0.2,1.2),
									cc.ScaleBy:create(0.3,0.1),
									cc.CallFunc:create(function( )
										baseWall[self.row][self.col] = 0
										if self.col> 1 then baseCanTurnMap[self.row][self.col-1][2] = 0 end 									--左边点不可右移 
										if self.col<8 then baseCanTurnMap[self.row][self.col+1][1] = 0 	end										--右边点不可左移 											
										--墙消失后，判断当前点的左右两边是否墙或者空，有的话标记当前点右左右移动，同时清除左右两边的移动标记
										if self.col> 1 and baseMap[self.row][self.col-1]==0 or baseWall[self.row][self.col-1]==1 then
											baseCanTurnMap[self.row][self.col][1] = 1
										end
										if self.col<8 and baseMap[self.row][self.col+1]==0 or baseWall[self.row][self.col+1]==1 then   --是否为有效点
											baseCanTurnMap[self.row][self.col][2] = 1
										end
										gSprites[self.row] = gSprites[self.row] or {}
										gSprites[self.row][self.col] = false
										self:removeSelf()
									end)
									)
								)
							end
						end)
						)
					)
				end
			end)
			)
		)
	end
end


return BaseSprite